package com.bw1806.sellergoods.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.bw1806.mapper.TbBrandMapper;
import com.bw1806.pojo.TbBrand;
import com.bw1806.pojo.TbBrandExample;
import com.bw1806.sellergoods.service.BrandService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import entity.PageResult;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * @描述
 * @创建人 wangyue
 * @创建时间2020/11/1115:02
 */
@Service(timeout = 5000,retries = 0)
public class BrandServiceImpl implements BrandService {

    @Autowired
    private TbBrandMapper tbBrandMapper;

    @Override
    public List<TbBrand> findAll() {
        return tbBrandMapper.selectByExample(null);
    }

    @Override
    public PageResult<TbBrand> pagelist(TbBrand tbBrand,int pageSize,int pageNum) {

        PageHelper.startPage(pageNum, pageSize);

        TbBrandExample example=new TbBrandExample();

        TbBrandExample.Criteria criteria = example.createCriteria();

        if(tbBrand!=null){
            if(tbBrand.getName()!=null && tbBrand.getName().length()>0){
                criteria.andNameLike("%"+tbBrand.getName()+"%");
            }
            if(tbBrand.getFirstChar()!=null && tbBrand.getFirstChar().length()>0){
                criteria.andFirstCharEqualTo(tbBrand.getFirstChar());
            }
        }

        Page<TbBrand> page= (Page<TbBrand>) tbBrandMapper.selectByExample(example);

        return new PageResult(page.getTotal(), page.getResult());
    }

    @Override
    public void save(TbBrand tbBrand) {
        Long id = tbBrand.getId();
        if(id != null && id !=0){
            tbBrandMapper.updateByPrimaryKey(tbBrand);
        } else{
            tbBrandMapper.insert(tbBrand);
        }
    }

    @Override
    public TbBrand getById(Long id) {
        return tbBrandMapper.selectByPrimaryKey(id);
    }

    @Override
    public void deletebth(Long[] ids) {
       /* for(long id : ids){
            tbBrandMapper.deleteByPrimaryKey(id);
        }*/
        tbBrandMapper.deletebth(ids);
    }
}
